<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="mb-20">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="输入关键词" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 文件表格 -->
    <el-table :data="fileList" border style="width: 100%">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="fileName" label="文件名" align="center" />
      <el-table-column prop="fileType" label="类型" width="100" align="center">
        <template #default="scope">
          <el-tag :type="getFileTagType(scope.row.fileType)">
            {{ scope.row.fileType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="uploadTime" label="上传时间" width="160" align="center">
        <template #default="scope">
          {{ formatTime(scope.row.uploadTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="uploadUserId" label="上传用户ID" width="120" align="center" />
      <el-table-column label="操作" width="220" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            size="mini"
            :disabled="!scope.row.fileUrl"
            @click="handlePreview(scope.row)"
          >
            预览
          </el-button>
          <el-button
            type="success"
            size="mini"
            :disabled="!scope.row.fileUrl"
            @click="handleDownload(scope.row)"
          >
            下载
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDeleteFile(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt-20"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getList, deleteFile } from '@/api/file' // 后期替换为真实接口
import moment from 'moment'

export default {
  name: 'FileRecord',
  data() {
    return {
      fileList: [], // 文件记录列表
      total: 0, // 总记录数
      pageSize: 10, // 每页数量
      currentPage: 1, // 当前页码
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取文件列表
    async fetchData() {
      try {
        const res = await getList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        })
        this.fileList = res.uploadfilePage.records
        this.total = res.uploadfilePage.total
      } catch (error) {
        this.$message.error('获取文件记录失败')
      }
    },
    // 格式化时间
    formatTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 文件类型标签样式
    getFileTagType(fileType) {
      const typeMap = {
        image: 'success',
        pdf: 'warning',
        doc: 'info',
        default: 'primary'
      }
      return typeMap[fileType.toLowerCase()] || typeMap.default
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        fileName: '',
        uploadUserId: null
      }
      this.currentPage = 1
      this.fetchData()
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 文件预览（根据文件类型判断是否支持在线预览）
    handlePreview(row) {
      if (!row.fileUrl) return
      const supportedTypes = ['image', 'pdf', 'jpeg', 'jpg', 'png']
      if (supportedTypes.includes(row.fileType.toLowerCase())) {
        window.open(row.fileUrl, '_blank')
      } else {
        this.$message.warning('该文件类型暂不支持在线预览')
      }
    },
    // 文件下载
    handleDownload(row) {
      if (!row.fileUrl) return
      const link = document.createElement('a')
      link.href = row.fileUrl
      link.download = row.fileName
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    },
    // 删除文件
    async handleDeleteFile(row) {
      try {
        await this.$confirm('确定删除该文件?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteFile({ fileId: row.fileId })
        this.$message.success('文件删除成功')
        this.fetchData() // 刷新列表
      } catch (error) {
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}

.mt-20 {
  margin-top: 20px;
}
</style>
